﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.UI
{
    public abstract class BasePage : System.Web.UI.Page
    {
        protected abstract void InitLogic();
        protected abstract void DisposeLogic();
                
        protected void AddScript(string js)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "customJS-" + Guid.NewGuid(), js, true);
        }

        /// <summary>
        /// 清理内容
        /// </summary>
        /// <param name="controls"></param>
        protected void ClearControlText(params Control[] controls)
        {
            foreach (var item in controls)
            {
                if (item is ITextControl)
                {
                    ((ITextControl)item).Text = string.Empty;
                }
                else if (item is HiddenField)
                {
                    ((HiddenField)item).Value = string.Empty;
                }
                else if (item is CheckBox)
                {
                    ((CheckBox)item).Checked = false;
                }
            }
        }

        /// <summary>
        /// 获取当前界面的操作类型
        /// </summary>
        /// <returns></returns>
        protected EditStateEnum GetAction()
        {
            string strAction = Request.QueryString["action"];
            if (string.IsNullOrEmpty(strAction))
            {
                return EditStateEnum.New;
            }
            else
            {
                try
                {
                    return (EditStateEnum)Enum.Parse(typeof(EditStateEnum), strAction, true);
                }
                catch (Exception ex)
                {
                    Loger.LogErr(ex);
                    return EditStateEnum.New;
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitLogic();
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            DisposeLogic();
        }
    }
}
